using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;

namespace ProcessesSMS
{
	/// <summary>
	/// Summary description for.
	/// Project name: SMS_xoso
	/// channel: n/a
	/// command code: DG X Y
	/// created: 01/09/09 .
	/// Writer : Nampt + Tuanbc
	/// Company: VIT TELECOM
	/// </summary>

	public class DAUGIA
	{
		#region Declare
		private SMS_MO WMO;
		private SMS_MT WMT;
		ArrayList arr = new ArrayList();
		public DAUGIA(SMS_MO MO)
		{
			WMO = MO;
		}

		public DAUGIA()
		{
		}
		#endregion
		public void run()
		{
			WMT = new SMS_MT(WMO);
			string tmpMsg = WMO.Message.Substring(WMO.CommandCode.Length).Trim().Replace("  ", " ").Replace("  ", " ");
			tmpMsg = tmpMsg.Replace("  ", " ").Trim().ToUpper();
			string[] msg = tmpMsg.Split(new char[] { ' ' });


			string strReturnMsg = "";            
			strReturnMsg = GetReturnMsg(msg);
			if(strReturnMsg!="")
			{
				WMT.AddMessage(WMO.UserID, strReturnMsg, SMS_ContentType.SMS_TEXT);
				WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
				WMT.Insert(); 
			}
			return;   
		}
		public string GetReturnMsg(string[] arr)
		{
			string msgBody="";
			string strSQL="";
			if (WMO.CommandCode.ToUpper()=="DG")
			{
				#region Cu phap DG
				if ((arr.Length==2 & WMO.ServiceID.Replace("04","")=="6572") || (arr.Length==2 & WMO.ServiceID.Replace("04","")=="6672") || (arr.Length==2 & WMO.ServiceID.Replace("04","")=="6772") || (arr.Length==2 & WMO.UserID=="84984696925" & WMO.ServiceID.Replace("04","")=="6072"))
				{
					// Ung voi Cu Phap DG <ProductCode> <Cost> gui 6572
					strSQL ="SELECT * from DG_PRODUCTS WHERE (CODE='" + arr[0] +"') AND (getdate() between DATE_START AND DATE_EXPIRE)";
					SqlConnection sqlconn = new SqlConnection(ConfigSettings.ReadSetting("STOCK_CONNECTION_STRING"));
					sqlconn.Open();
					SqlDataAdapter da = new SqlDataAdapter(strSQL, sqlconn);
					DataSet ds = new DataSet();
					da.Fill(ds);
					if (ds.Tables[0].Rows.Count==0)
					{
						msgBody="Chao ban. De dau gia san pham ban vui long soan tin theo cu phap DG masanpham gia gui den 6572. Vi du soan tin: DG E71 300 gui 6572.";
					}
					else
					{
						msgBody="Cam on ban da tham gia dich vu dau gia cua chung toi. Ban da dau gia cho san pham co ma la: " + arr[0] + "\nco ten la: " + ConvertStr(ds.Tables[0].Rows[0]["Name"].ToString()) + "\nvoi gia dau la: " + arr[1]+".000 dong";
						// Thuc hien cau lenh insert User vao Insert_DG_USERS
						// Insert_DG_USERS(string _USERID, string _CODE, int _PRICE, System.DateTime _REQUEST_TIME, int _STATUS)
						Insert_DG_USERS(WMO.UserID.Trim(), arr[0], Int32.Parse(arr[1])*1000, WMO.RequestTime, 0);
						
					}
				}
				else if (arr.Length==1)
				{
					if ((arr[0].ToUpper()=="HOTRO") & (WMO.ServiceID.Replace("04","")=="6172") || (arr[0].ToUpper()=="HOTRO") & (WMO.ServiceID.Replace("04","")=="6272") || (arr[0].ToUpper()=="HOTRO") & (WMO.ServiceID.Replace("04","")=="6372") || (arr[0].ToUpper()=="HOTRO") & (WMO.ServiceID.Replace("04","")=="6472") || (arr[0].ToUpper()=="HOTRO") & (WMO.ServiceID.Replace("04","")=="6572") || (arr[0].ToUpper()=="HOTRO") & (WMO.ServiceID.Replace("04","")=="6672") || (arr[0].ToUpper()=="HOTRO") & (WMO.ServiceID.Replace("04","")=="6772") || (arr[0].ToUpper()=="HOTRO") & (WMO.ServiceID.Replace("04","")=="6072") & (WMO.UserID=="84984696925"))
					{
						// Ung voi Cu phap DG HOTRO gui 6172
						strSQL = "SELECT TOP 1 * from DG_PRODUCTS WHERE getdate() between DATE_START AND DATE_EXPIRE";
						SqlConnection sqlconn = new SqlConnection(ConfigSettings.ReadSetting("STOCK_CONNECTION_STRING"));
						sqlconn.Open();
						SqlDataAdapter da = new SqlDataAdapter(strSQL, sqlconn);
						DataSet ds = new DataSet();
						da.Fill(ds);
						if (ds.Tables[0].Rows.Count>0)
						{
							DateTime sttemp = Convert.ToDateTime(ds.Tables[0].Rows[0]["DATE_START"].ToString());
							int stngay = sttemp.Day;
							int stthang = sttemp.Month;
							int stnam = sttemp.Year;

							DateTime edtemp = Convert.ToDateTime(ds.Tables[0].Rows[0]["DATE_EXPIRE"].ToString());
							int edngay = edtemp.Day;
							int edthang = edtemp.Month;
							int ednam = edtemp.Year;

							msgBody="Chung toi dang thuc hien dau gia: " + ds.Tables[0].Rows[0]["NAME"];
							msgBody += "\nSan pham nay co ma la: " + ConvertStr(ds.Tables[0].Rows[0]["CODE"].ToString());
							msgBody += "\nPhien dau gia nay bat dau tu: " + stngay + "/" + stthang + "/" + stnam;
							msgBody += " va ket thuc vao: " + edngay + "/" + edthang + "/" + ednam;
						}
					}
					else
					{
						// Ung voi Cu phap DG <lungtungxeng> gui 6172
						msgBody = "Chao ban. De dau gia san pham ban vui long soan tin theo cu phap DG masanpham gia gui den 6572. Vi du soan tin: DG E71 300 gui 6572";
					}
				}
				else
				{
					msgBody = "Chao ban. De dau gia san pham ban vui long soan tin theo cu phap DG masanpham gia gui den 6572. Vi du soan tin: DG E71 300 gui 6572";
				}
				#endregion
			}
			else if ((WMO.CommandCode.ToUpper()=="KQ" & WMO.ServiceID.Replace("04","")=="6172") || (WMO.CommandCode.ToUpper()=="KQ" & WMO.ServiceID.Replace("04","")=="6272") || (WMO.CommandCode.ToUpper()=="KQ" & WMO.ServiceID.Replace("04","")=="6372") || (WMO.CommandCode.ToUpper()=="KQ" & WMO.ServiceID.Replace("04","")=="6472") || (WMO.CommandCode.ToUpper()=="KQ" & WMO.ServiceID.Replace("04","")=="6572") || (WMO.CommandCode.ToUpper()=="KQ" & WMO.ServiceID.Replace("04","")=="6672") || (WMO.CommandCode.ToUpper()=="KQ" & WMO.ServiceID.Replace("04","")=="6772"))
			{
				#region Cu phap KQ <ProductCode> <Cost>
				if (arr.Length==2)
				{
					strSQL = "begin";
					strSQL += " declare @dem int";
					strSQL += " set @dem = (select count(*) from DG_USERS WHERE CODE='" + arr[0] +"' AND PRICE='" + arr[1] + "')";
					strSQL += " select TOP 1 CODE, PRICE, @dem from DG_USERS WHERE CODE='" + arr[0] + "' AND PRICE='" + arr[1] + "'";
					strSQL += " end";
				
					SqlConnection sqlconn = new SqlConnection(ConfigSettings.ReadSetting("STOCK_CONNECTION_STRING"));
					sqlconn.Open();			
					SqlDataAdapter da = new SqlDataAdapter(strSQL, sqlconn);
					DataSet ds = new DataSet();
					da.Fill(ds);
					sqlconn.Close();
					if (ds.Tables[0].Rows.Count>0)
					{
						msgBody = "San pham co MA la: " + ds.Tables[0].Rows[0][0].ToString() + "\nHien tai dang co: " + ds.Tables[0].Rows[0][2].ToString() + " nguoi cung tra gia voi muc gia la: " + ds.Tables[0].Rows[0][1].ToString();
					}
					else
					{
						msgBody = "De biet co bao nhieu nguoi trung y tuong voi ban vui long soan tin KQ masanpham gia gui 6172. Vi du soan tin: KQ E71 300 gui 6172";
					}
				}
				else
				{
					msgBody = "De biet co bao nhieu nguoi trung y tuong voi ban vui long soan tin KQ masanpham gia gui 6172. Vi du soan tin: KQ E71 300 gui 6172";
				}
				#endregion
			}
			else
			{
				msgBody = "Chao ban. De dau gia san pham ban vui long soan tin theo cu phap DG masanpham gia gui den 6572. Vi du soan tin: DG E71 300 gui 6572";
			}
			return msgBody;
		}
		private void Insert_DG_USERS(string _USERID, string _CODE, int _PRICE, System.DateTime _REQUEST_TIME, int _STATUS)
		{
			string strSQLInsert="";
			strSQLInsert = "INSERT INTO DG_USERS(USERID, CODE, PRICE, REQUEST_TIME, STATUS)";
			strSQLInsert += " VALUES(@USERID, @CODE, @PRICE, @REQUEST_TIME, @STATUS)";
			SqlConnection cnn = new SqlConnection("server=115.84.178.97;uid=sa;pwd=hieutc;database=VIT_SEL");
			cnn.Open();

			SqlCommand cmd;
			cmd = new SqlCommand(strSQLInsert,cnn);			
			cmd.Parameters.Clear();

			cmd.Parameters.Add("@USERID", _USERID);
			cmd.Parameters.Add("@CODE", _CODE);
			cmd.Parameters.Add("@PRICE", _PRICE);
			cmd.Parameters.Add("@REQUEST_TIME", _REQUEST_TIME);	
			cmd.Parameters.Add("@STATUS", _STATUS);
					
			cmd.ExecuteNonQuery();
			cmd.Dispose();
			cnn.Close();				
		}
		// check ky tu dac biet va replace
		public static string ConvertStr(string str)
		{
			string tg1 = "áàạãảăằắẳặãâấầẩẫậ";
			string tg2 = "éèẻẽẹêếềểễệ";
			string tg3 = "óòỏõọôốồổỗộơớờởỡợ";
			string tg4 = "íìỉĩị";
			string tg5 = "úùủũụưứừửữự";
			string tg6 = "ýỳỷỹỵ";
			//string tg7 = "đ";
			//
			int i;
			char tg;
			//cat cac ky tu dac biet
			string strSpecial = "!@#$%^&*()':;?>-<//\\\".,`~";
			for (i = 0; i < strSpecial.Length; i++)
			{
				tg = strSpecial[i];
				str = str.Replace(tg, ' ');
			}
			//chuyen thanh chu thuong
			str = str.ToLower();
			//cat khoang trang
			str = str.Trim();           
			str = str.Replace(' ', '_');
			str = str.Replace("___", "_");
			str = str.Replace("__", "_");
			for (i = 0; i < tg1.Length; i++)
			{
				tg = tg1[i];
				str= str.Replace(tg, 'a');
			}

			for (i = 0; i < tg2.Length; i++)
			{
				tg = tg2[i];
				str = str.Replace(tg, 'e');
			}

			for (i = 0; i < tg3.Length; i++)
			{
				tg = tg3[i];
				str = str.Replace(tg, 'o');
			}

			for (i = 0; i < tg4.Length; i++)
			{
				tg = tg4[i];
				str = str.Replace(tg, 'i');
			}

			for (i = 0; i < tg5.Length; i++)
			{
				tg = tg5[i];
				str = str.Replace(tg, 'u');
			}

			for (i = 0; i < tg6.Length; i++)
			{
				tg = tg6[i];
				str = str.Replace(tg, 'y');
			}
			str = str.Replace('đ','d');            

			return str;
		}
	}		
}
